<!--用户管理-->
<template>
  <div class="page-container">
    <ex_super_table_page v-bind="option" ref="SuperTablePage"  :cell-class-name="cellClassMethod" @selection-change="e => selection = e">
    </ex_super_table_page>


    <el-dialog :title="$tt('label_fms_currency_info_config')" :visible.sync="dialogFormVisible" width="520px"
      height="200px" :close-on-click-modal="false">

      <el-form ref="submitFormEl" :model="submitForm" :rules="rules" label-width="120px" class="common-form-panel-div"
        style="padding: 0px 10px; margin-top: 20px; ">

        <el-row>
          <el-col>
            <el-form-item :label="$tt('label_fms_currency_basic_coin')" prop="localCurrency">
              <ex_super_input type="select" enum="CurrencyTypeEnum" :placeholder="$tt('tip_please_select')"
                v-model="submitForm.localCurrency" style="width: 100%;" />
            </el-form-item>
          </el-col>

          <el-col style="width: 30px; color: red; padding-top: 6px; padding-left: 8px; cursor: pointer; ">
            <el-tooltip class="item" effect="light" :content="$tt('label_fms_currency_warning')" placement="top-start">
              <i class="el-icon-warning"></i>
            </el-tooltip>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item :label="$tt('label_fms_currency_rate_update_exchange')" prop="period">
              <el-select v-model="submitForm.period" placeholder="" style="width: 100%;">
                <el-option v-for="item in [{ label: '每周更新一次', value: 'week' }, { label: '每天更新一次', value: 'day' }]"
                  :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>


        <el-row v-if="submitForm.period == 'week'">
          <el-col>
            <el-form-item :label="$tt('label_fms_currency_update_date')" prop="weekday">

              <el-select v-model="submitForm.weekday" placeholder="" style="width: 100%;">
                <el-option
                  v-for="item in [{ label: '周一', value: '1' }, { label: '周二', value: '2' }, { label: '周三', value: '3' }, { label: '周四', value: '4' }, { label: '周五', value: '5' }, { label: '周六', value: '6' }, { label: '周日', value: '7' }]"
                  :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $tt('label_fms_account_config_cancel') }}</el-button>
        <el-button type="primary" @click="doSubmitForm">{{ $tt('label_fms_account_config_confirm') }}</el-button>
      </div>

    </el-dialog>


  </div>
</template>
<script>

let moduleName = "label_fms_currency_manage"

import FmsCurrencyChoose from '@/component/fms/fms-currency-choose.vue'

import superTablePageFunction from '@/mixin/super-table-page-function';

export default {

  title: moduleName,
  components: { FmsCurrencyChoose, },

  mixins: [superTablePageFunction],
  data() {

    let _this = this

    return {

      dialogFormVisible: false,
      dialogTitle: '',
      formValue: {
        'type': 'DONE'
      },
      rules: {
        // companyName: [
        //   {required: true, message: '请输入企业名称', trigger: 'blur'},
        //   {min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur'}
        // ],
        // companyShortCode: [
        //   {required: true, message: '请输入企业编码', trigger: 'blur'},
        //   {min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur'}
        // ]
      },


      submitForm: {

      },
      customerAccountOptions: []


    };
  },


  watch: {

  },

  created() {

    let _this = this

    _this.option = {
      search: {
        model: [
          { label: "label_fms_currency_transaction_coin", key: "currencyTypes", type: 'select', enum: 'CurrencyTypeEnum', multiple: true },
          { label: "label_fms_account_apply_status", key: "status", type: 'select', enum: 'IsAvailableStatusEnum' },
        ],
      },
      actionExtra: [
        {
          label: 'label_fms_currency_correlation_config',
          auth: 'fms_config_currency_edit',
          attrs: { icon: "el-icon-setting" },
          click: () => {
            _this.initCurrentForm()
          }
        },
        { label: 'label_enable_disable', attrs: { type: "primary" }, click: this.enableDisableFn },
      ],
      table: {
        url: "/fms-api/agent/AgentCurrencyRpc/selectByPage.json",
        attrs: {
          'cell-style': ({ row, column, rowIndex, columnIndex }) => {
            if (column.property == 'status') {
              return { color: row.status == 'ENABLE' ? this.$globalCss.successColor : this.$globalCss.infoColor }
            }
          }
        },
        model: [
          { type: 'selection', clickToggle: true, },
          {
            key: 'currencyType', label: 'label_fms_currency_transaction_coin', align: 'left', attrs: {
              formatter: (r, c, v, i) => {
                return this.$enumShow(r.currencyType, 'CurrencyTypeEnum') + '  ' + r.currencyType
              }
            }
          },
          {
            key: 'localCurrency', label: 'label_fms_currency_basic_coin', align: 'left', attrs: {
              formatter: (r, c, v, i) => {
                return this.$enumShow(r.localCurrency, 'CurrencyTypeEnum') + '  ' + r.localCurrency
              }
            }
          },
          {
            key: 'openRate', label: 'label_fms_currency_official_exchange_rate', align: 'right'
          },
          {
            key: 'localRate', label: 'label_fms_currency_custom_exchange_rate', align: 'right',
          },
          {
            key: 'lastUpdateDate', label: 'label_fms_currency_rate_last_update_date', align: 'right',
          },
          {
            key: 'status', label: 'label_fms_account_apply_status',
            attrs: {
              formatter: (r, c, v, i) => {
                return this.$enumShow(r.status, 'IsAvailableStatusEnum')
              }
            }
          },
          {
            type: 'operate',
            operate: [
              {
                label: 'IsAvailableStatusEnum__DISABLED',
                auth: 'fms_config_currency_disable',

                type: "confirm-button",
                message: "label_fms_bank_card_confirm_forbidden",
                url: '/fms-api/agent/AgentCurrencyRpc/disableCurrency.json',
                if: (row) => {
                  return row.status == 'ENABLE'
                },
                params: (v) => {
                  return { currencyId: v.id }
                },
                done: ['reload', 'tip'],
              },
              {
                label: 'IsAvailableStatusEnum__ENABLE',
                auth: 'fms_config_currency_disable',

                type: "confirm-button",
                message: "label_fms_bank_card_confirm_enable",
                url: '/fms-api/agent/AgentCurrencyRpc/enableCurrency.json',
                if: (row) => {
                  return row.status != 'ENABLE'
                },
                params: (v) => {
                  return { currencyId: v.id }
                },
                done: ['reload', 'tip']
              }
            ],
          }
        ]
      }
    }

  },

  props: {},

  destroyed() {

  },

  methods: {
    cellClassMethod({ row, column, rowIndex, columnIndex }) {
      if (column.property == 'status') {
        if (row.status == 'ENABLE') {
          return 'color_success'
        } else if (row.status == 'DISABLED') {
          return 'color_danger'
        }
      }
      return ""
    },
    initCurrentForm() {
      let _this = this
      _this.$ajax({
        url: '/fms-api/agent/AgentConfigRpc/getAgentConfig.json',
        success: (res) => {
          if (!res.data) {
            _this.$message.error('数据异常')
            return;
          }
          _this.submitForm = res.data
          _this.dialogFormVisible = true
        }
      });

    },

    doSubmitForm() {

      let _this = this

      _this.$refs['submitFormEl'].validate((valid) => {

        if (valid) {

          _this.$ajax({
            url: '/fms-api/agent/AgentConfigRpc/updateAgentConfig.json',
            // type: 'json',
            data: _this.submitForm,
            success: (responseData) => {
              this.$message({
                showClose: true,
                message: '提交成功',
                type: 'success'
              })
              _this.$refs['SuperTablePage'].reload()
              _this.dialogFormVisible = false
            }
          });

        } else {
          return false;
        }

      });

    },
    enableDisableFn() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }

      const ENABLE = this.selection.every(item => item.status === 'ENABLE')
      const DISABLED = this.selection.every(item => item.status === 'DISABLED')
      if (ENABLE || DISABLED) {
        const msg = DISABLED ? this.$t('label_fms_bank_card_confirm_enable') : this.$t('label_fms_bank_card_confirm_forbidden')
        this.$confirm(msg, this.$t('label_tip'), {
          type: 'warning'
        }).then(() => {
          this.$ajax({
            url: "/fms-api/agent/AgentCurrencyRpc/enableOrDisableBatch.json",
            method: 'post', type: 'json',
            loading: true,
            data: {
              ids: this.selection.map(it => it.id),
              status: ENABLE ? 'DISABLED' : 'ENABLE'
            },
            success: (res) => {
              this.$message.success(this.$tt('tip_operate_success'))
              this.reload()
            },
          })
        })

      } else {
        this.$message.error(this.$tt('label_tms_country_batch_error'))
      }

    },

  }
};

</script>
